<?php
namespace Home\Controller;
use Think\Controller;
use Org\Net\Http;
use Think\Image;
class IndexController extends Controller {
    public function index(){
    	$this->display();
    }
    public function download(){
    	$this->display();
    }
    public function otherScenic(){
    	$this->display();
    }
    public function add(){ 
    	 $files = $_FILES;
    	$code = 0;
    	if(sizeof($files)<4){
    		$this->error("图片不能少于4张！");
    	}
    	foreach ($files as $file){
			$uploadpath = "Public/Upload/Scenic/";//上传路径

			$util = A('Util');
			$util->createDir($uploadpath);//上传路径是否已经存在，不存在则创建

			$pinfo = pathinfo($file['name']);
			$ftype = $pinfo['extension'];//文件类型
			$name = basename($file['name'],".{$ftype}");

			$fileName = md5($name.time())."_{$code}.".$ftype;//识别图
			$image = new Image();
			$image->open($file['tmp_name']);
			$image->crop(400, 300,0,0)->save($uploadpath.$fileName);
			$image_url .= "http://qianmosen.cn/tubu/".$uploadpath.$fileName.",";
			$code++;
    	}		
    	$http = new Http();
    	$post['latitude'] = $_POST['lat'];
    	$post['longitude'] = $_POST['long'];
    	$post['geotable_id'] = '111203';
    	$post['ak'] = C("GSBAIDU_AK");
    	$post['coord_type'] = 3;
    	$post['address'] = $_POST['address'];
    	$post['title'] = $_POST['scenicName'];
    	$data['post'] = $post;
    	$back = $http->fsockopenDownload(C("BAIDU_POI_CREATE"),$data);
    	$back = json_decode($back,'json');
    	if($back['status']==0){
    		$scenic = D('Scenic');
    		$scenicData=array(
    				'name'=>$_POST['scenicName'],
    				'subtitle'=>$_POST['title'],
    				'address' =>$_POST['address'],
    				'phone'   =>$_POST['phone'],
    				'intro'   =>$_POST['intro'],
    				'latitude'=>$_POST['lat'],
    				'longitude'=>$_POST['long'],
    				'image'	   =>$image_url,
    				'type'	   =>$_POST['type'],
    		);
    		if($scenic->create($scenicData)){
    			$scenic->add();
    			$sid = M('Scenic')->where(array('name'=>$_POST['scenicName']))->getField('id');
    			$ticketInfo="";
    			foreach ($_POST['ticketPrice'] as $k=>$v){
    				$ticketInfo=$ticketInfo.$_POST['ticketName'][$k].':'.$_POST['ticketPrice'][$k].',';
    				}
    			if(M('otherTicket')->add(array('scenic_id'=>$sid,'ticket_info'=>$ticketInfo))){
    				$this->success("添加成功！");
    			}else{
    				$this->error("添加失败！");
    			}
    		}
    		
    	} 
     }
    public function get(){
    	$http = new Http();
    	$geotable_id ='111203';
        $city = '温州';
    	$url = 'http://api.map.baidu.com/geosearch/v3/local'."?ak=".C('GSBAIDU_AK')."&geotable_id={$geotable_id}&page_size=10&region={$city}";
    	$back =  $http->fsockopenDownload($url);
    	$back = json_decode($back,'json');
    	if($back['status']==0){
    		echo "成功";
    	}
    }
}